package com.sphd.beautysalon.activitys.funding;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.beautysalon.adapters.InvestorItemAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.InvestorInfo;
import com.sphd.beautysalon.info.InvestorItemInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.AccountDetailPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.AccountDetailView;
import com.sphd.beautysalon.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 个人明细
 */
public class AccountDetailsActivity extends BaseActivity implements AccountDetailView, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.money_record)
    TextView moneyRecord;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.all_my_money)
    TextView allMyMoney;
    @BindView(R.id.m_year_cut_money)
    TextView mYearCutMoney;
    @BindView(R.id.m_cut_money)
    TextView mCutMoney;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    @BindView(R.id.deal)
    Button deal;
    @BindView(R.id.content_account_details)
    LinearLayout contentAccountDetails;
    AccountDetailPresenterImpl detailPresenter;
    Intent intent;
    Bundle bundle;
    InvestorInfo investorInfo;
    RestaurantInfo restaurantInfo;
    InvestorItemAdapter invesAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_details);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());

        mySwf.setOnRefreshListener(this);
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
//                bundle.putSerializable(Constant.INVESTOR,restaurantInfo);
                bundle.putSerializable(Constant.INVESTORITEM, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context,MonthDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        investorInfo = (InvestorInfo) bundle.getSerializable(Constant.INVESTOR);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        detailPresenter = new AccountDetailPresenterImpl(this);
        setTitleBar(toolbar, investorInfo.getAccount_name());
        setRefresh(mySwf);
        setRecy(myRecy);
        initAdapter();
        addData();
    }

    private void initAdapter() {
        invesAdapter = new InvestorItemAdapter(R.layout.item_accout_details, new ArrayList<InvestorItemInfo>());
        myRecy.setAdapter(invesAdapter);
    }

    private void addData() {
        detailPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.MANAGERACCOUNT + "cgid=" + restaurantInfo.getId() + "&phone=" +
                investorInfo.getPhone() + "&messageid=" + investorInfo.getId() + "&staffid=" + investorInfo.getAccount_staffid());
    }


    @OnClick({R.id.money_record, R.id.deal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.money_record:
                //资金记录
                intent.setClass(this, MoneyRecordActivity.class);
                startActivityForResult(intent, 1);

                break;
            case R.id.deal:
                intent.putExtras(bundle);
                intent.putExtra(Constant.TAG, context.getClass().getSimpleName());
                intent.setClass(context, DealActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void setmCutMoney(double all_interest) {
        mCutMoney.setText(turn(all_interest,Constant.DECIMAL_POINT_TWO));
    }

    @Override
    public void rfAdapter(List<InvestorItemInfo> investorItemInfos) {
        invesAdapter.setNewData(investorItemInfos);
    }

    @Override
    public void setMoney(List<InvestorInfo> investorInfoList) {
        allMyMoney.setText(turn(investorInfoList.get(0).getInvestment()));
        mYearCutMoney.setText(turn(investorInfoList.get(0).getInterest()));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            setResult(1);
            finish();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        addData();
    }
}
